<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.userId" placeholder="请选择用户" prop="userId"
                            type="select" :load="userRemoteDataUrl"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.status" placeholder="请选择状态" prop="status"
                            type="select"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.startTime" type="date"
                            placeholder="开始时间"></globalSearchTemplate>
      <globalSearchTemplate :config="globalConfig" v-model="listQuery.endTime" type="date"
                            placeholder="结束时间"></globalSearchTemplate>
      <el-button class="filter-item" v-waves @click="handleSearch" icon="el-icon-search">搜索</el-button>
      <el-button class="filter-item" v-if="userDrawManager_btn_add" v-waves @click="handleCreate" type="primary"
                 icon="edit">添加
      </el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit
              highlight-current-row style="width: 100%">
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="编号" prop="serialNumber" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="用户" prop="userId" type="select" :load="userRemoteDataUrl" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="金额" prop="drawAmount" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="状态" prop="status" type="select" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="创建日期" prop="createDate" width="170"></globalTableColumn>
      <globalTableColumn :data="list" :loading.sync="listLoading" :key="key" :config="globalConfig" align="center"
                         label="备注" prop="memo" width="270"></globalTableColumn>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button v-if="userDrawManager_btn_upd" size="small" type="success"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-if="userDrawManager_btn_del" size="mini" type="danger"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="listQuery.page"
                     :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="35%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <globalFormItem :key="formKey" :config="globalConfig" label="用户" v-model="form.userId" prop="userId"
                        placeholder="请选择用户" type="select" :load="userRemoteDataUrl"></globalFormItem>
        <globalFormItem :key="formKey" :config="globalConfig" label="提现金额" v-model="form.drawAmount" prop="drawAmount"
                        placeholder="请输入提现金额"></globalFormItem>

        <globalFormItem :key="formKey" :config="globalConfig" label="提现状态" v-model="form.status" prop="status"
                        placeholder="请选择提现状态" type="select"></globalFormItem>
        <el-form-item label="备注" prop="memo" style="width: 420px">
          <el-input v-model="form.memo" :rows="6" type="textarea" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .filter-item {
    margin-left: 10px;
  }
</style>

<script>
  import { fetchList, getObj, addObj, putObj, delObj, casObj } from '@/api/user/userDraw'
  import waves from '@/directive/waves/index.js' // 水波纹指令
  import { mapGetters } from 'vuex'
  import { globalNotify } from '@/util/notifyUtils'  //弹窗
  import { getToken } from '@/util/auth'
  import globalSearchTemplate from '@/components/global-el-search-template'
  import globalTableColumn from '@/components/global-el-table-column'
  import globalFormItem from '@/components/global-el-form-item'
  import { remoteData as userRemoteData } from '@/api/user/usersInfo'

  export default {
    name: 'userDraw',
    components: { globalSearchTemplate, globalTableColumn, globalFormItem },
    directives: {
      waves
    },
    data() {
      return {
        userRemoteDataUrl: userRemoteData,
        globalConfig: {
          module: ['user'],
          componentName: ['userDraw']
        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          'startTime': undefined,
          'endTime': undefined,
          'userId': undefined,
          'drawAmount': undefined,
          'status': undefined,
          'memo': undefined
        },
        rules: {},
        form: {
          'userId': undefined,
          'drawAmount': undefined,
          'status': undefined,
          'memo': undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        userDrawManager_btn_add: false,
        userDrawManager_btn_upd: false,
        userDrawManager_btn_del: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0,
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        casDataUrl: casObj,
        key: '',
        formKey: ''
      }
    },
    computed: {
      ...mapGetters([
        'permissions'
      ])
    },
    filters: {},
    created() {
      this.getList()
      this.userDrawManager_btn_add = this.permissions['userDrawManager_btn_add']
      this.userDrawManager_btn_upd = this.permissions['userDrawManager_btn_upd']
      this.userDrawManager_btn_del = this.permissions['userDrawManager_btn_del']
    },
    methods: {
      getList() {
        this.listLoading = true
        fetchList(this.listQuery).then(response => {
            this.list = response.data.data.records
            this.refreshTable()
            this.total = response.data.data.total
            this.listLoading = false
          }
        )
      },
      handleSizeChange(val) {
        this.listQuery.limit = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getList()
      },
      handleDelete(row) {
        delObj(row.id)
          .then(response => {
              this.dialogFormVisible = false
              this.getList()
              globalNotify(this, response)
            }
          )
      },
      handleCreate() {
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.resetForm()
        this.refreshForm()
      },
      refreshTable() {
        this.key = new Date().getTime()
      },
      refreshForm() {
        this.formKey = new Date().getTime()
      },
      handleUpdate(row) {
        this.resetForm()
        getObj(row.id).then(response => {
          this.form = response.data.data
          this.refreshForm()
          this.dialogFormVisible = true
          this.dialogStatus = 'update'
        })

      },
      create(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              addObj(this.form)
                .then((response) => {
                    this.dialogFormVisible = false
                    this.getList()
                    globalNotify(this, response)
                  }
                )
            } else {
              return false
            }
          }
        )
      },
      cancel(formName) {
        this.resetForm()
        this.dialogFormVisible = false
      },
      update(formName) {
        const set = this.$refs
        set[formName].validate(valid => {
            if (valid) {
              this.dialogFormVisible = false
              this.form.password = undefined
              putObj(this.form).then((response) => {
                  this.dialogFormVisible = false
                  this.getList()
                  globalNotify(this, response)
                }
              )
            } else {
              return false
            }
          }
        )
      },
      resetForm() {
        this.form = {
          'userId': undefined,
          'drawAmount': undefined,
          'status': undefined,
          'memo': undefined
        }
      },
      handleSearch() {
        this.listQuery.page = 1
        this.getList()
      }
    }
  }
</script>
